package com.itheima.controller;

import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {


//    @Resource(name = "deptServiceImpl") //根据bean的名字注入对象，等价于 @Autowired+@Qualifier
    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门，获取部门列表数据
     * @return
     */
    @GetMapping("/depts")       //限制请求方式为Get
    public Result list(){
        //1.调用service，获取数据
        List<Dept> depts = deptService.list();

        //3.响应数据（json格式）
        return Result.success(depts);
    }

    //删除 部门

//    方式一：
//    @GetMapping("/depts/delete",method = RequestMethod.Delete)
/*    @DeleteMapping("/depts")
    public Result delete(HttpServletRequest  request){
        String id = request.getParameter("id");
        System.out.println(Integer.valueOf(id));
        return Result.success();
    }*/

/*
//    方式二：通过Spring提供的@RequestParam注解获取参数
    @DeleteMapping("/depts")
    public Result delete(@RequestParam("id") Integer deptId){
        System.out.println("deptId="+deptId);
        return Result.success();
    }
*/

//    方式三：通过SpringMVC封装的参数对象
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        System.out.println("deptId=:" + id);
        deptService.delete(id);
        return Result.success();
    }

//    新增
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        System.out.println("dept="+dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("根据ID查询部门数据="+ id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        System.out.println("dept = " + dept);
        deptService.update(dept);
        return Result.success(dept);

    }

}
